package com.qupeng.demo.kafka.kafkaspringbootconsumer.mm2.client;

import jakarta.annotation.Resource;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.connect.mirror.RemoteClusterUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;

@RestController
@RequestMapping("/mm2/client")
public class MM2ClientController {

    @Resource(name = "mm2ClientConsumerConfigs")
    private Map<String, Object> mm2ClientConsumerConfigs;

    @GetMapping("/translate-offsets")
    public void translateOffsets() {
        try {
            Set<String> topics = RemoteClusterUtils.checkpointTopics(mm2ClientConsumerConfigs);
            Map<TopicPartition, OffsetAndMetadata> destinationOffsetsMap = RemoteClusterUtils.translateOffsets(mm2ClientConsumerConfigs,
                    "msghub-uat-a", "mirrormaker2-consumer", Duration.ofMillis(10000));
            System.out.println(destinationOffsetsMap);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }
    }
}
